/**
 * Created by Administrator on 2016/7/11.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, template, Model, dialog, datepicker) {
    var other_file = 0;
    var index = function () {
        this.init();
    };

    index.prototype = {

        init: function () {
            var _this = this;

            //地区
            $.ajax({
                url: _global.url.api + 'regions',
                type: 'get',
                async: false,
                data: {depth: 2},
                success: function (res) {
                    if (res.code == 0) {
                        _this.set('region', res.data);
                    }
                }
            });

            this.fetch();
            this.event();
        },
        fetch: function () {
            var _this = this;

            $('#main-view').html(template('add-tpl', {region: this.region}));

            this.model = new Model({
                url: _global.url.api + 'agent/recommend',
                data: {}
            });
        },
        set: function ($k, $v) {
            this[$k] = $v;
        },
        event: function () {
            var _this = this;
            //保存事件
            $('#btn-save').on('click', function (e) {
                e.preventDefault();
                _this.model.data.other_file = other_file;
                _this.model.data.name = $('[name="name"]').val();
                _this.model.data.contact = $('[name="contact"]').val();
                _this.model.data.tel = $('[name="tel"]').val();
                _this.model.data.mobile = $('[name="mobile"]').val();
                _this.model.data.city = $('[name="city"]').val();

                if (_this.model.post()) {
                    _this.fetch();
                } else {

                }
            });
            //地区联动
            $('.province').on('change', function () {
                $('select[name="city"]').html('');
                var id = $(this).val();
                var city = _this.region[id].child;
                for (var n in city) {
                    $('select[name="city"]').append('<option value="' + city[n].id + '">' + city[n].name + '</option>')
                }
            });

            $('input,select,textarea').on('change', function (e) {
                _this.change(e);
            });

            //上传附件

            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var affix = $("#upload-frame")[0].contentWindow.affix.get();
                        _this.affixPreview(affix);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },
        change: function (e) {
            this.model.data[$(e.target).attr('name')] = $(e.target).val();
        },
        affixPreview: function (affix) {
            console.log('this.data', affix);
            if (typeof affix.id !== 'undefined') {
                other_file = affix.id;
                $('#img-view').html(template('affix-tpl', affix));
            }
        }
    };

    new index();

});